home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gpp-1_42.lha / g++-1.42.0 / newcrt0.c < prev    next >
C/C++ Source or Header  |  1991-10-19  |  22KB  |  768 lines

  1. /* C++ code startup routine.
  2.    Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3.    Hacked by Michael Tiemann (tiemann@mcc.com)
  4.  
  5.                NO WARRANTY
  6.  
  7.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  8. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  9. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  10. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  11. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  12. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  14. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  15. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  16. CORRECTION.
  17.  
  18.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  19. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  20. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  21. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  22. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  23. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  24. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  25. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  26. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  27. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  28.  
  29.         GENERAL PUBLIC LICENSE TO COPY
  30.  
  31.   1. You may copy and distribute verbatim copies of this source file
  32. as you receive it, in any medium, provided that you conspicuously and
  33. appropriately publish on each copy a valid copyright notice "Copyright
  34. (C) 1986 Free Software Foundation, Inc."; and include following the
  35. copyright notice a verbatim copy of the above disclaimer of warranty
  36. and of this License.
  37.  
  38.   2. You may modify your copy or copies of this source file or
  39. any portion of it, and copy and distribute such modifications under
  40. the terms of Paragraph 1 above, provided that you also do the following:
  41.  
  42.     a) cause the modified files to carry prominent notices stating
  43.     that you changed the files and the date of any change; and
  44.  
  45.     b) cause the whole of any work that you distribute or publish,
  46.     that in whole or in part contains or is a derivative of this
  47.     program or any part thereof, to be licensed at no charge to all
  48.     third parties on terms identical to those contained in this
  49.     License Agreement (except that you may choose to grant more extensive
  50.     warranty protection to some or all third parties, at your option).
  51.  
  52.     c) You may charge a distribution fee for the physical act of
  53.     transferring a copy, and you may at your option offer warranty
  54.     protection in exchange for a fee.
  55.  
  56. Mere aggregation of another unrelated program with this program (or its
  57. derivative) on a volume of a storage or distribution medium does not bring
  58. the other program under the scope of these terms.
  59.  
  60.   3. You may copy and distribute this program (or a portion or derivative
  61. of it, under Paragraph 2) in object code or executable form under the terms
  62. of Paragraphs 1 and 2 above provided that you also do one of the following:
  63.  
  64.     a) accompany it with the complete corresponding machine-readable
  65.     source code, which must be distributed under the terms of
  66.     Paragraphs 1 and 2 above; or,
  67.  
  68.     b) accompany it with a written offer, valid for at least three
  69.     years, to give any third party free (except for a nominal
  70.     shipping charge) a complete machine-readable copy of the
  71.     corresponding source code, to be distributed under the terms of
  72.     Paragraphs 1 and 2 above; or,
  73.  
  74.     c) accompany it with the information you received as to where the
  75.     corresponding source code may be obtained.  (This alternative is
  76.     allowed only for noncommercial distribution and only if you
  77.     received the program in object code or executable form alone.)
  78.  
  79. For an executable file, complete source code means all the source code for
  80. all modules it contains; but, as a special exception, it need not include
  81. source code for modules which are standard libraries that accompany the
  82. operating system on which the executable file runs.
  83.  
  84.   4. You may not copy, sublicense, distribute or transfer this program
  85. except as expressly provided under this License Agreement.  Any attempt
  86. otherwise to copy, sublicense, distribute or transfer this program is void and
  87. your rights to use the program under this License agreement shall be
  88. automatically terminated.  However, parties who have received computer
  89. software programs from you with this License Agreement will not have
  90. their licenses terminated so long as such parties remain in full compliance.
  91.  
  92.   5. If you wish to incorporate parts of this program into other free
  93. programs whose distribution conditions are different, write to the Free
  94. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  95. worked out a simple rule that can be stated here, but we will often permit
  96. this.  We will be guided by the two goals of preserving the free status of
  97. all derivatives of our free software and of promoting the sharing and reuse of
  98. software.
  99.  
  100.  
  101. In other words, you are welcome to use, share and improve this program.
  102. You are forbidden to forbid anyone else to use, share and improve
  103. what you give them.   Help stamp out software-hoarding!  */
  104.  
  105.  
  106. /* The standard Vax 4.2 Unix crt0.c cannot be used for Emacs
  107.    because it makes `envron' an initialized variable.
  108.    It is easiest to have a special crt0.c on all machines
  109.    though I don't know whether other machines actually need it.  */
  110.  
  111. /* Also, the standard crt0.c cannot be used for C++, since C++
  112.    guarantees to call global constructors before main () is
  113.    called, and to call global destructors after control has left from
  114.    main ().  */
  115.  
  116. /* On the vax and 68000, in BSD4.2 and USG5.2,
  117.    this is the data format on startup:
  118.   (vax) ap and fp are unpredictable as far as I know; don't use them.
  119.   sp ->  word containing argc
  120.          word pointing to first arg string
  121.      [word pointing to next arg string]... 0 or more times
  122.      0
  123. Optionally:
  124.      [word pointing to environment variable]... 1 or more times
  125.      ...
  126.      0
  127. And always:
  128.      first arg string
  129.      [next arg string]... 0 or more times
  130. */
  131.  
  132. /* On the 16000, at least in the one 4.2 system I know about,
  133.   the initial data format is
  134.   sp ->  word containing argc
  135.          word containing argp
  136.          word pointing to first arg string, and so on as above
  137. */
  138.  
  139. /* On Suns, and possibly on other machines, a routine called
  140.    `on_exit' is used to call termination handling routines.
  141.    If your system provides this feature, define the macro
  142.    ON_EXIT to perform this task.  It is called after a  program
  143.    calls exit(3)  or  returns  normally,  and before its process
  144.    terminates.  The routine named is called as
  145.  
  146.         (*procp)(status, arg);
  147.  
  148.    where status is the argument with which exit was called,  or
  149.    zero  if  main returns.  Typically, arg is the address of an
  150.    argument vector to (*procp), but may be  an  integer  value.
  151.    Several  calls  may  be  made to ON_EXIT, specifying several
  152.    termination handlers. The order in which they are called  is
  153.    the reverse of that in which they were given to ON_EXIT.
  154.  
  155.    Some systems may impose a limit on the number of termination
  156.    handlers which can be specified.  Users near that limit are
  157.    warned that GNU C++ will take one of those slots.
  158.  
  159.    Note that proper GNU C++ code will not need to rely on this
  160.    mechanism at the application level, because thoughtfully
  161.    constructed classes will take care of this automatically.
  162.    There is no limit to the number of classes which GNU C++
  163.    can destroy.  :-) */
  164.  
  165. #include "config.h"
  166.  
  167. /*        ********  WARNING ********
  168.     Do not insert any data definitions before data_start!
  169.     Since this is the first file linked, the address of the following
  170.     variable should correspond to the start of initialized data space.
  171.     On some systems this is a constant that is independent of the text
  172.     size for shared executables.  On others, it is a function of the
  173.     text size. In short, this seems to be the most portable way to
  174.     discover the start of initialized data space dynamically at runtime,
  175.     for either shared or unshared executables, on either swapping or
  176.     virtual systems.  It only requires that the linker allocate objects
  177.     in the order encountered, a reasonable model for most Unix systems.
  178.     Similarly, note that the address of _start() should be the start
  179.     of text space.   Fred Fish, UniSoft Systems Inc.  */
  180.  
  181. int data_start = 0;
  182.    
  183. #ifdef NEED_ERRNO
  184. int errno = 0;
  185. #endif
  186.  
  187. #ifndef DONT_NEED_ENVIRON 
  188. char **environ;
  189. #endif
  190.  
  191. /* These are for GNU C++.  */
  192. #include "crt0.h"
  193. extern void exit ();
  194. extern void __do_global_cleanup ();
  195.  
  196. #if defined(sequent)
  197.     asm("    .globl _387_flt");
  198.     asm("    .set    _387_flt,0");
  199. #endif
  200.  
  201. #if defined(orion) || defined(pyramid) || defined(celerity) || defined(ALLIANT)
  202.  
  203. #ifdef ALLIANT
  204. /* _start must initialize _curbrk and _minbrk on the first startup;
  205.    when starting up after dumping, it must initialize them to what they were
  206.    before the dumping, since they are in the shared library and
  207.    are not dumped.  See ADJUST_EXEC_HEADER in m-alliant.h.  */
  208. extern unsigned char *_curbrk, *_minbrk;
  209. extern unsigned char end;
  210. unsigned char *_setbrk = &end;
  211. #endif
  212.  
  213. _start (argc, argv, envp)
  214.      int argc;
  215.      char **argv, **envp;
  216. {
  217. #ifdef ALLIANT
  218.   _curbrk = _setbrk;
  219.   _minbrk = _setbrk;
  220. #endif
  221.  
  222.   environ = envp;
  223.  
  224.   __do_global_init ();
  225.   exit (main (argc, argv, envp));
  226. }
  227.  
  228. #endif /* orion or pyramid or celerity or alliant */
  229.  
  230. #if defined (ns16000) && !defined (sequent) && !defined (UMAX)
  231.  
  232. _start ()
  233. {
  234. /* On 16000, _start pushes fp onto stack */
  235.   start1 ();
  236. }
  237.  
  238. /* ignore takes care of skipping the fp value pushed in start.  */
  239. static
  240. start1 (ignore, argc, argv)
  241.      int ignore;
  242.      int argc;
  243.      register char **argv;
  244. {
  245.   environ = argv + argc + 1;
  246.  
  247.   if (environ == *argv)
  248.     environ--;
  249.  
  250.   __do_global_init ();
  251.   exit (main (argc, argv, environ));
  252. }
  253. #endif /* ns16000, not sequent and not UMAX */
  254.  
  255. #ifdef UMAX
  256. _start()
  257. {
  258.     asm("    exit []            # undo enter");
  259.     asm("    .set    exitsc,1");
  260.     asm("    .set    sigcatchall,0x400");
  261.  
  262.     asm("    .globl    _exit");
  263.     asm("    .globl    start");
  264.     asm("    .globl    __start");
  265.     asm("    .globl    _main");
  266.     asm("    .globl    _environ");
  267.     asm("    .globl    _sigvec");
  268.     asm("    .globl    sigentry");
  269.  
  270.     asm("start:");
  271.     asm("    br    .xstart");
  272.     asm("    .org    0x20");
  273.     asm("    .double    p_glbl,0,0xf00000,0");
  274.     asm("    .org    0x30");
  275.     asm(".xstart:");
  276.     asm("    adjspb    $8");
  277.     asm("    movd    8(sp),0(sp)    # argc");
  278.     asm("    addr    12(sp),r0");
  279.     asm("    movd    r0,4(sp)    # argv");
  280.     asm("L1:");
  281.     asm("    movd    r0,r1");
  282.     asm("    addqd    $4,r0");
  283.     asm("    cmpqd    $0,0(r1)    # null args term ?");
  284.     asm("    bne    L1");
  285.     asm("    cmpd    r0,0(4(sp))    # end of 'env' or 'argv' ?");
  286.     asm("    blt    L2");
  287.     asm("    addqd    $-4,r0        # envp's are in list");
  288.     asm("L2:");
  289.     asm("    movd    r0,8(sp)    # env");
  290.     asm("    movd    r0,@_environ    # indir is 0 if no env ; not 0 if env");
  291.     asm("    movqd    $0,tos        # setup intermediate signal handler");
  292.     asm("    addr    @sv,tos");
  293.     asm("    movzwd    $sigcatchall,tos");
  294.     asm("    jsr    @_sigvec");
  295.     asm("    adjspb    $-12");
  296.     asm("    jsr    @___do_global_init");
  297.     asm("    jsr    @_main");
  298.     asm("    adjspb    $-12");
  299.     asm("    movd    r0,tos");
  300.     asm("    jsr    @___do_global_cleanup");
  301.     asm("    jsr    @_exit");
  302.     asm("    adjspb    $-4");
  303.     asm("    addr    @exitsc,r0");
  304.     asm("    svc");
  305.     asm("    .align    4        # sigvec arg");
  306.     asm("sv:");
  307.     asm("    .double    sigentry");
  308.     asm("    .double    0");
  309.     asm("    .double    0");
  310.  
  311.     asm("    .comm    p_glbl,1");
  312. }
  313. #endif /* UMAX */
  314.  
  315. #ifdef CRT0_DUMMIES
  316.  
  317. /* Define symbol "start": here; some systems want that symbol.  */
  318. #ifdef DOT_GLOBAL_START
  319. asm("    .text        ");
  320. asm("    .globl start    ");
  321. asm("    start:        ");
  322. #endif /* DOT_GLOBAL_START */
  323.  
  324. #ifdef NODOT_GLOBAL_START
  325. asm("    text        ");
  326. asm("    global start    ");
  327. asm("    start:        ");
  328. #endif /* NODOT_GLOBAL_START */
  329.  
  330. _start ()
  331. {
  332. /* On vax, nothing is pushed here  */
  333. /* On sequent, bogus fp is pushed here  */
  334.   start1 ();
  335. }
  336.  
  337. static
  338. start1 (CRT0_DUMMIES argc, xargv)
  339.      int argc;
  340.      char *xargv;
  341. {
  342.   register char **argv = &xargv;
  343.   environ = argv + argc + 1;
  344.  
  345.   if ((char *)environ == xargv)
  346.     environ--;
  347.  
  348.   __do_global_init ();
  349.   exit (main (argc, argv, environ));
  350. }
  351. #else /* not CRT0_DUMMIES */
  352.  
  353. /* "m68k" and "m68000" both stand for m68000 processors,
  354.    but with different program-entry conventions.
  355.    This is a kludge.  Now that the CRT0_DUMMIES mechanism above exists,
  356.    most of these machines could use the vax code above
  357.    with some suitable definition of CRT0_DUMMIES.
  358.    Then the symbol m68k could be flushed.
  359.    But I don't want to risk breaking these machines
  360.    in a version 17 patch release, so that change is being put off.  */
  361.  
  362. #ifdef m68k            /* Can't do it all from C */
  363.     asm ("    global    _start");
  364.     asm ("    text");
  365.     asm ("_start:");
  366. #ifndef NU
  367. #ifdef STRIDE
  368.     asm ("    comm    havefpu%,2");
  369. #else /* m68k, not STRIDE */
  370.     asm ("    data");
  371.     asm ("    even");
  372.     asm ("    global    splimit%");
  373.     asm ("splimit%:");
  374.     asm ("    space 4");
  375. #endif /* STRIDE */
  376.     asm ("    global    exit");
  377.     asm ("    text");
  378. #ifdef STRIDE
  379.     asm ("    trap    &3");
  380.     asm ("    mov.w    %d0,havefpu%");
  381. #else /* m68k, not STRIDE */
  382.       asm ("    mov.l    %d0,splimit%");
  383. #endif /* STRIDE */
  384. #endif /* not NU */
  385.     asm ("    jsr    start1");
  386.     asm ("    mov.l    %d0,(%sp)");
  387.     asm ("    jsr    exit");
  388.     asm ("    mov.l    &1,%d0");    /* d0 = 1 => exit */
  389.     asm ("    trap    &0");
  390. #else /* m68000, not m68k */
  391.  
  392. #if defined(m68000) || defined(mc68000) || defined(mc68020)
  393.   
  394. #ifdef ISI68K
  395. /* Added by ESM Sun May 24 12:44:02 1987 to get new ISI library to work */
  396.     asm ("    .globl  is68020");
  397.     asm ("is68020:");
  398.     asm ("    .long   0x00000000");
  399.     asm ("    .long   0xffffffff");
  400. /* End of stuff added by ESM */
  401.     asm ("    .text");
  402.     asm ("    .globl    __start");
  403.     asm ("__start:");
  404.     asm ("    .word 0");
  405.     asm ("    link    fp,#0");
  406.     asm ("    jbsr    _start1");
  407.     asm ("    unlk    fp");
  408.     asm ("    rts");
  409. #else /* not ISI68K */
  410. _start ()
  411. {
  412. /* On 68000, _start pushes a6 onto stack  */
  413.   start1 ();
  414. }
  415. #endif /* not ISI68k */
  416. #endif /* m68000 || mc68000 || mc68020 */
  417. #endif /* m68k */
  418.  
  419. #if defined(sun)
  420. #define ON_EXIT(PROCP, ARG) \
  421.   do { extern void PROCP (); on_exit (PROCP, ARG); } while (0)
  422. #endif
  423.  
  424. #if defined(m68k) || defined(m68000) || defined(mc68000) || defined(mc68020)
  425. /* ignore takes care of skipping the a6 value pushed in start.  */
  426. static
  427. #if defined(m68k)
  428. start1 (argc, xargv)
  429. #else
  430. start1 (ignore, argc, xargv)
  431. #endif
  432.      int argc;
  433.      char *xargv;
  434. {
  435.   register char **argv = &xargv;
  436.   environ = argv + argc + 1;
  437.  
  438.   if ((char *)environ == xargv)
  439.     environ--;
  440.  
  441. #ifdef ON_EXIT
  442.  
  443. #ifdef sun
  444.   ON_EXIT (_cleanup, 0);
  445. #endif
  446.  
  447.   ON_EXIT (__do_global_cleanup, 0);
  448.  
  449. #endif
  450.  
  451.   __do_global_init ();
  452.   exit (main (argc, argv, environ));
  453. }
  454.  
  455. #endif /* m68k or m68000 or mc68000 or mc68020 */
  456.  
  457. #endif /* not CRT0_DUMMIES */
  458.  
  459. /* Should "hp9000" be completely removed?  */
  460. #if defined(hp9000) || defined(hp9000s300)
  461. int argc_value;
  462. char **argv_value;
  463. #ifdef OLD_HP_ASSEMBLER
  464.     asm("   text");
  465.     asm("    globl __start");
  466.     asm("    globl _exit");
  467.     asm("    globl _main");
  468.     asm("__start");
  469.     asm("    dc.l    0");
  470.     asm("    subq.w    #0x1,d0");
  471.     asm("    move.w    d0,float_soft");
  472.     asm("    move.l    0x4(a7),d0");
  473.     asm("    beq.s    skip_1");
  474.     asm("    move.l    d0,a0");
  475.     asm("    clr.l    -0x4(a0)");
  476.     asm("skip_1");
  477.     asm("    move.l    a7,a0");
  478.     asm("    subq.l    #0x8,a7");
  479.     asm("    move.l    (a0),(a7)");
  480.     asm("    move.l    (a0),_argc_value");
  481.     asm("    addq.l    #0x4,a0");
  482.     asm("    move.l    a0,0x4(a7)");
  483.     asm("    move.l    a0,_argv_value");
  484.     asm("incr_loop");
  485.     asm("    tst.l    (a0)+");
  486.     asm("    bne.s    incr_loop");
  487.     asm("    move.l    0x4(a7),a1");
  488.     asm("    cmp.l    (a1),a0");
  489.     asm("    blt.s    skip_2");
  490.     asm("    subq.l    #0x4,a0");
  491.     asm("skip_2");
  492.     asm("    move.l    a0,0x8(a7)");
  493.     asm("    move.l    a0,_environ");
  494.     asm("    jsr    ___do_global_init");
  495.     asm("    jsr    _main");
  496.     asm("    addq.l    #0x8,a7");
  497.     asm("    move.l    d0,-(a7)");
  498.     asm("    jsr    _exit");
  499.     asm("    move.w    #0x1,d0");
  500.     asm("    trap    #0x0");
  501.     asm("    comm    float_soft,4");
  502. /* float_soft is allocated in this way because C would
  503.    put an underscore character in its name otherwise. */ 
  504.  
  505. #else /* new hp assembler */
  506.  
  507.     asm("    text");
  508.     asm("    global    __start");
  509.     asm("    global    _exit");
  510.     asm("    global    _main");
  511.         asm("   global  flag_fpa,flag_68010,fpa_loc,float_loc");
  512.     asm("__start:");
  513.     asm("    byte    0,0,0,0");
  514.     asm("   sub.l   %a6,%a6");
  515.     asm("    subq.w    &1,%d0");
  516.     asm("    mov.w    %d0,float_soft");
  517.     asm("    mov.w    %d1,flag_68881");
  518.     asm("    beq.b    skip_float"); 
  519.     asm("    fmov.l    &0x7400,%fpcr");
  520.     asm("skip_float:");
  521.     asm("   move.l  %a0,%d0");
  522.     asm("   add.l   %d0,%d0");
  523.     asm("   subx.w  %d1,%d1");
  524.     asm("   move.w  %d1,flag_68010");
  525.     asm("   add.l   %d0,%d0");
  526.     asm("   subx.w  %d1,%d1");
  527.     asm("   move.w  %d1,flag_fpa");
  528.     asm("    mov.l    4(%a7),%d0");
  529.     asm("    beq.b    skip_1");
  530.     asm("    mov.l    %d0,%a0");
  531.     asm("    clr.l    -4(%a0)");
  532.     asm("skip_1:");
  533.     asm("    mov.l    %a7,%a0");
  534.     asm("    subq.l    &8,%a7");
  535.     asm("    mov.l    (%a0),(%a7)");
  536.     asm("    mov.l    (%a0),_argc_value");
  537.     asm("    addq.l    &4,%a0");
  538.     asm("    mov.l    %a0,4(%a7)");
  539.     asm("    mov.l    %a0,_argv_value");
  540.     asm("incr_loop:");
  541.     asm("    tst.l    (%a0)+");
  542.     asm("    bne.b    incr_loop");
  543.     asm("    mov.l    4(%a7),%a1");
  544.     asm("    cmp.l    %a0,(%a1)");
  545.     asm("    blt.b    skip_2");
  546.     asm("    subq.l    &4,%a0");
  547.     asm("skip_2:");
  548.     asm("    mov.l    %a0,8(%a7)");
  549.     asm("    mov.l    %a0,_environ");
  550.     asm("    jsr    ___do_global_init");
  551.     asm("    jsr    _main");
  552.     asm("    addq.l    &8,%a7");
  553.     asm("    mov.l    %d0,-(%a7)");
  554.     asm("    jsr    _exit");
  555.     asm("    mov.w    &1,%d0");
  556.     asm("    trap    &0");
  557.     asm("    comm    float_soft, 4");
  558.     asm("    comm    flag_68881, 4");
  559.     asm("    comm    flag_68010, 4");
  560.     asm("    comm    flag_fpa,   4");
  561.     asm("   set     float_loc,0xffffb000");
  562.     asm("   set     fpa_loc,0xfff08000");
  563.  
  564. #endif /* new hp assembler */
  565. #endif /* hp9000 */
  566.  
  567. #ifdef GOULD
  568.  
  569. /* startup code has to be in near text rather
  570.    than fartext as allocated by the C compiler. */
  571.     asm("    .text");
  572.     asm("    .align    2");
  573.     asm("    .globl    __start");
  574.     asm("    .text");
  575.     asm("__start:");
  576. /* setup base register b1 (function base). */
  577.     asm("    .using    b1,.");
  578.     asm("    tpcbr    b1");
  579. /* setup base registers b3 through b7 (data references). */
  580.     asm("    file    basevals,b3");
  581. /* setup base register b2 (stack pointer); it should be
  582.    aligned on a 8-word boundary; but because it is pointing
  583.    to argc, its value should be remembered (in r5). */
  584.     asm("    movw    b2,r4");
  585.     asm("    movw    b2,r5");
  586.     asm("    andw    #~0x1f,r4");
  587.     asm("    movw    r4,b2");
  588. /* allocate stack frame to do some work. */
  589.     asm("    subea    16w,b2");
  590. /* initialize signal catching for UTX/32 1.2; this is 
  591.    necessary to make restart from saved image work. */
  592.     asm("    movea    sigcatch,r1");
  593.     asm("    movw    r1,8w[b2]");
  594.     asm("    svc    #1,#150");
  595. /* setup address of argc for start1. */
  596.     asm("    movw    r5,8w[b2]");
  597.     asm("   func    #1,_start1");
  598.     asm("    halt");
  599. /* space for ld to store base register initial values. */
  600.     asm("    .align    5");
  601.     asm("basevals:");
  602.     asm("    .word    __base3,__base4,__base5,__base6,__base7");
  603.  
  604. static
  605. start1 (xargc)
  606.      int *xargc;
  607. {
  608.   register int    argc;
  609.   register char **argv;
  610.  
  611.   argc = *xargc;
  612.   argv = (char **)(xargc) + 1;
  613.   environ = argv + argc + 1;
  614.  
  615.   if (environ == argv)
  616.     environ--;
  617.  
  618.   __do_global_init ();
  619.   exit (main (argc, argv, environ));
  620. }
  621.  
  622. #endif /* GOULD */
  623.  
  624. #ifdef elxsi
  625. extern int errno;
  626. extern char **environ;
  627.  
  628. _start()
  629. {
  630.   errno = 0;
  631.   environ = *(&environ + 8);
  632.   _stdinit();
  633.  
  634.   __do_global_init ();
  635.   exit (main(*(&environ + 6), *(&environ + 7), environ));
  636. }
  637. #endif /* elxsi */
  638.  
  639. #ifdef sparc
  640. asm (".global start");
  641. asm (".text");
  642. asm ("start:");
  643.  
  644. /* Set up `argc', `argv', and `envp' into local registers.  */
  645. asm ("    mov    0, %fp");
  646. asm ("    ld    [%sp + 64], %l0");
  647. asm ("    add    %sp, 68, %l1");
  648. asm ("    sll    %l0, 2,    %l2");
  649. asm ("    add    %l2, 4,    %l2");
  650. asm ("    add    %l1, %l2, %l2");
  651. asm ("    sethi    %hi(_environ), %l3");
  652. asm ("    st    %l2, [%l3+%lo(_environ)]");
  653.  
  654. #ifdef ON_EXIT
  655.  
  656. #ifdef sun
  657. asm ("    set __cleanup,%o0");
  658. asm ("    call _on_exit,0");
  659. asm ("    mov %g0,%o1");
  660. #endif
  661.  
  662. asm ("    set ___do_global_cleanup,%o0");
  663. asm ("    call _on_exit,0");
  664. asm ("    mov %g0,%o1");
  665.  
  666. #endif
  667.  
  668. /* Finish diddling with stack and call `__do_global_init'.  */
  669. asm ("    andn    %sp, 7,    %sp");
  670. asm ("    sub    %sp, 24, %sp");
  671. asm ("    call    ___do_global_init");
  672.  
  673. /* Move `argc', `argv', and `envp' from locals to parameters for `main'.  */
  674. asm ("    mov    %l0,%o0");
  675. asm ("    mov    %l1,%o1");
  676. asm ("    call    _main");
  677. asm ("    mov    %l2,%o2");
  678.  
  679. #ifndef ON_EXIT
  680. /* Save return value from `main', and call `__do_global_cleanup',
  681.    if necessary.  In any event, get return value from `main' into
  682.    a safe register.  */
  683. asm ("    call    ___do_global_cleanup");
  684. #endif
  685. asm ("    mov    %o0,%l0");
  686.  
  687. #ifdef ON_EXIT
  688. /* Call `exit' or `_exit' with return value from `main'.  */
  689. asm ("    call    _exit");
  690. #else
  691. asm ("    call    __exit");
  692. #endif
  693. asm ("    mov    %l0,%o0");
  694. #endif /* sparc */
  695.  
  696. #ifndef sun
  697. /* For C++, calls to exit(3) must perform their cleanup duties.
  698.    This means calling destructors on all of the items which
  699.    need to have their destructors called.  After calling these
  700.    destructors, a call is made to _exit (2), which is serious
  701.    business.  */
  702. void
  703. exit (status)
  704.      int status;
  705. {
  706.   __do_global_cleanup ();
  707. #ifdef hp9000s300
  708.   _cleanup ();
  709. #endif
  710.   _exit (status);
  711. }
  712. #endif
  713.  
  714. static int _end_crt0 ();
  715.  
  716. void
  717. __do_global_init ()
  718. {
  719.   extern void (*__CTOR_LIST__)();
  720.   register void (**ppf)() = &__CTOR_LIST__;
  721.   register int *pi = (int *)ppf;
  722.  
  723. #ifdef VIRTUAL_FUNCTION_MASK
  724.   if ((int)_end_crt0 < VINDEX_MAX)
  725.     {
  726.       printf ("virtual function index too large--encroaches text space at address 0x%x\n", _end_crt0);
  727.       abort ();
  728.     }
  729. #else
  730.   /* @@What to do for losing segmented architectures?  */
  731. #endif
  732.  
  733.   while (*pi++)
  734.     {
  735.       /* Losing PCC cannot handle this statement as (*ppf++)().  Pity.  */
  736.       (*ppf)();
  737.       ppf++;
  738.     }
  739. }
  740.  
  741. typedef struct dtor_table_entry
  742. {
  743.   struct dtor_table_entry *next;
  744.   void (*fp)();
  745. } dtor_table_entry;
  746.  
  747. extern dtor_table_entry *__DTOR_LIST__;
  748.  
  749. void
  750. __do_global_cleanup ()
  751. {
  752.   register int i;
  753.  
  754.   while (__DTOR_LIST__)
  755.     {
  756.       /* Prevent problems if a destructor should mistakenly call
  757.      exit() by always consuming one entry per round.  */
  758.       void (*fp)() = __DTOR_LIST__->fp;
  759.       __DTOR_LIST__ = __DTOR_LIST__->next;
  760.       (*fp)();
  761.     }
  762. }
  763.  
  764. static int
  765. _end_crt0 ()
  766. {
  767. }
  768.